package m.member.manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.html.VueFormResult;
import m.html.VueListResult;
import m.html.element.Element;
import m.html.element.attr.Align;
import m.html.element.attr.ButtonType;
import m.html.form.*;
import m.html.list.ListButton;
import m.html.list.ListCount;
import m.html.list.ListElement;
import m.html.list.SuccessMethod;
import m.member.manage.model.AppSetting;
import m.member.manage.model.MemberLevel;
import m.member.manage.service.MemberLevelService;
import m.system.cache.CacheUtil;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import manage.action.StatusAction;
import manage.util.DictionaryUtil;

@ActionMeta(name = "member/manage/memberLevel")
public class MemberLevelAction extends StatusAction {
    private MemberLevel model;
    public JSONMessage doSave() throws Exception {
        verifyAdminOperPower(getStatusPower());
        String msg=getService(MemberLevelService.class).save(model);
        return new JSONMessage(0)
                .push("msg",msg);
    }
    public VueResult toEdit() throws Exception {
        verifyAdminOperPower(getStatusPower());
        if(ObjectUtil.isEmptyOid(model)){
            model=new MemberLevel();
            model.setColor("#c41d7f");
        }else{
            model= ModelQueryList.getModel(model,"*");
        }
        ModelForm form=new ModelForm("会员级别",2);
        form.addItem(
            FormInput.input("会员名称","model.name").setPlaceholder("请输入会员名称").setRequired(true),
            FormInput.number("优惠比例","model.discount","%").setPlaceholder("请输入优惠比例").setRequired(true),
            FormTest.test("model.name",
                FormHtml.ins("颜色",
                    Element.tag("a-tag").A("v-for","item in dictOptions('color')")
                        .A(":color","model.color==item.value?item.value:item.label")
                        .A("@click","model.color=item.value").E("{{model.name}}")
                ).setSpan(2)
            ),
            FormInput.textarea("备注","model.remark").setPlaceholder("请输入备注").setSpan(2)
        )
        .addData("model", model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.verify("verifyModel",
                FormJsTest.falseRemind("this.model.name","会员名称不能为空"),
                FormJsTest.falseRemind("this.model.discount&&parseInt(this.model.discount)>=0&&parseInt(this.model.discount)<=100","优惠比例范围错误")
            ),
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .before("verifyModel").success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
//        以上写法可根据系统设置统一样式，下面写法是前端页面写法
//        return new VuePageResult("page/member/manage/memberLevel.js")
//                .addParam("model", model);
    }

    @Override
    public void doRecoveryCallback() throws Exception {
        DictionaryUtil.clear(MemberLevel.class);
    }

    @Override
    public void doDisableCallback() throws Exception {
        DictionaryUtil.clear(MemberLevel.class);
    }

    /*
     * 会员级别
     */
    public VueResult allList() throws Exception {
        boolean power=hasAdminOperPower(getStatusPower());
        VueListResult result=getVueListResult("会员级别",MemberLevel.class,actionUrl("allList"));
        result.setFunction(true,false);
        result.addCol(toIndexColumn());
        result.addQueryFields("name","color");
        result.addCol(toElementColumn("会员名称", new ListElement("a-tag")
            .addAttr(":color","record.color")
            .addText("{{record.name}}")
        ).setAlign(Align.CENTER));
        result.addCol(toIntegerColumn("优惠比例%", "discount", 100, true).setCount(ListCount.NONE));
        result.addCol(toTextColumn("备注", "remark", 200, true));
        //状态列，第一个参数 是否可操作
        result.addCol(toStatusColumn(power, true));
        if(power) {
            result.addCol(toSpaceColumn("操作", 100,
                ListButton.modal("修改", ButtonType.PRIMARY, actionUrl("toEdit"), 800, SuccessMethod.REFRESH)
                    .addParam("model.oid", "oid")
            ).setFixed(Align.RIGHT));
            result.addButton(ListButton.modal("新增", ButtonType.PRIMARY, actionUrl("toEdit"), 800, SuccessMethod.REFRESH));
        }
        //排序 result.addOrder
        //查询条件 result.addCondition
        return result;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{"app_setting_power"};
    }
    public MemberLevel getModel() {
        return model;
    }

    public void setModel(MemberLevel model) {
        this.model = model;
    }
}
